<template>
  <div class="system-permission">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>权限管理</span>
          <el-button type="primary" @click="handleAdd">添加权限</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="权限名称">
          <el-input v-model="searchForm.name" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="权限类型">
          <el-select v-model="searchForm.type" placeholder="请选择类型">
            <el-option label="目录" value="menu" />
            <el-option label="菜单" value="page" />
            <el-option label="按钮" value="button" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 权限列表 -->
      <el-table
        :data="permissionList"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children' }"
      >
        <el-table-column prop="name" label="权限名称" min-width="200" />
        <el-table-column prop="code" label="权限标识" min-width="150" />
        <el-table-column prop="type" label="类型" width="100">
          <template #default="scope">
            <el-tag :type="getTypeTag(scope.row.type)">
              {{ scope.row.type }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="路由路径" min-width="150" />
        <el-table-column prop="icon" label="图标" width="100">
          <template #default="scope">
            <el-icon v-if="scope.row.icon">
              <component :is="scope.row.icon"></component>
            </el-icon>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="80" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button 
              type="primary" 
              size="small" 
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button 
              type="success" 
              size="small" 
              @click="handleAdd(scope.row)"
            >
              添加子级
            </el-button>
            <el-button 
              type="danger" 
              size="small" 
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑权限对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="600px"
    >
      <el-form :model="permissionForm" :rules="rules" ref="permissionFormRef" label-width="100px">
        <el-form-item label="上级权限">
          <el-tree-select
            v-model="permissionForm.parentId"
            :data="permissionOptions"
            :props="{ label: 'name', value: 'id' }"
            placeholder="请选择上级权限"
            check-strictly
            :render-after-expand="false"
          />
        </el-form-item>
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permissionForm.name" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="权限类型" prop="type">
          <el-radio-group v-model="permissionForm.type">
            <el-radio label="目录">目录</el-radio>
            <el-radio label="菜单">菜单</el-radio>
            <el-radio label="按钮">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限标识" prop="code" v-if="permissionForm.type === '按钮'">
          <el-input v-model="permissionForm.code" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item label="路由路径" prop="path" v-if="permissionForm.type !== '按钮'">
          <el-input v-model="permissionForm.path" placeholder="请输入路由路径" />
        </el-form-item>
        <el-form-item label="图标" v-if="permissionForm.type !== '按钮'">
          <el-select v-model="permissionForm.icon" placeholder="请选择图标">
            <el-option label="首页" value="HomeFilled" />
            <el-option label="设置" value="Setting" />
            <el-option label="用户" value="User" />
            <el-option label="列表" value="List" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="permissionForm.sort" :min="0" :max="999" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  HomeFilled,
  Setting,
  User,
  List
} from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  name: '',
  type: ''
})

// 权限列表数据
const permissionList = ref([
  {
    id: 1,
    name: '系统管理',
    code: '',
    type: '目录',
    path: '/system',
    icon: 'Setting',
    sort: 1,
    children: [
      {
        id: 11,
        name: '用户管理',
        code: '',
        type: '菜单',
        path: '/system/user',
        icon: 'User',
        sort: 1,
        children: [
          {
            id: 111,
            name: '添加用户',
            code: 'system:user:add',
            type: '按钮',
            path: '',
            icon: '',
            sort: 1
          },
          {
            id: 112,
            name: '编辑用户',
            code: 'system:user:edit',
            type: '按钮',
            path: '',
            icon: '',
            sort: 2
          }
        ]
      },
      {
        id: 12,
        name: '角色管理',
        code: '',
        type: '菜单',
        path: '/system/role',
        icon: 'List',
        sort: 2
      }
    ]
  }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 权限表单对话框
const dialogVisible = ref(false)
const dialogTitle = ref('添加权限')
const permissionFormRef = ref(null)
const permissionForm = reactive({
  parentId: null,
  name: '',
  type: '菜单',
  code: '',
  path: '',
  icon: '',
  sort: 0
})

const rules = {
  name: [
    { required: true, message: '请输入权限名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择权限类型', trigger: 'change' }
  ],
  code: [
    { required: true, message: '请输入权限标识', trigger: 'blur' },
    { pattern: /^[a-z][a-z0-9:]*$/, message: '格式：小写字母开头，可包含数字和冒号', trigger: 'blur' }
  ],
  path: [
    { required: true, message: '请输入路由路径', trigger: 'blur' },
    { pattern: /^\/[a-z/]*$/, message: '格式：以/开头的小写字母路径', trigger: 'blur' }
  ],
  sort: [
    { required: true, message: '请输入排序号', trigger: 'blur' }
  ]
}

// 权限树选项
const permissionOptions = ref([
  {
    id: 0,
    name: '顶级权限',
    children: permissionList.value
  }
])

// 方法定义
const handleSearch = () => {
  ElMessage.success('搜索功能待实现')
}

const resetSearch = () => {
  searchForm.name = ''
  searchForm.type = ''
}

const getTypeTag = (type) => {
  const types = {
    '目录': 'primary',
    '菜单': 'success',
    '按钮': 'info'
  }
  return types[type] || 'info'
}

const handleAdd = (row) => {
  dialogTitle.value = '添加权限'
  Object.assign(permissionForm, {
    parentId: row?.id || 0,
    name: '',
    type: '菜单',
    code: '',
    path: '',
    icon: '',
    sort: 0
  })
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑权限'
  Object.assign(permissionForm, row)
  dialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该权限吗？删除后其下级权限也会被删除', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
  })
}

const submitForm = async () => {
  if (!permissionFormRef.value) return
  
  await permissionFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success(dialogTitle.value === '添加权限' ? '添加成功' : '更新成功')
      dialogVisible.value = false
    }
  })
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 